using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class Utilities
    {
        private Utilities(){ }

        public static HSDPrincipal CreateUser(System.Xml.XmlNode userNode, System.Xml.XmlNamespaceManager nsmgr)
        {
            string userName = userNode.SelectSingleNode("//pnp:UserName", nsmgr).InnerText;
            string password = userNode.SelectSingleNode("//pnp:Password", nsmgr).InnerText;

            return business.SecurityManager.ValidateUser(userName, password);
        }


        //ValidateUser = 2000
        public static void ValidateUser(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            HSDPrincipal principal = Utilities.CreateUser(userCredentials, nsmgr);

            resultXmlMessage = new XmlDocument();

            //create a responsePayload string from the principal object if it isn't null:
            if (principal != null)
            {
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(principal, typeof(HSDPrincipal)));
            }

            resultMsgType = (int)MessageTypes.PrincipalResult;
        }
    }
}
